
#import "Card_PlayerSelected.h"
#import "Card_Game.h"

#import "AppController.h"

@implementation Card_PlayerSelected

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Card_PlayerSelected *layer = [Card_PlayerSelected node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

- (void) dealloc
{
	[playerInfoArray release];
	
	[[NSNotificationCenter defaultCenter] removeObserver:self 
													name:@"ChangNum" 
												  object:nil];
	
	[super dealloc];
}

-(id)init:(id<Card_PlayerSelectedDelegate>) _delegate
{
	self = [super init];
	if (self)
	{
		AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
		
		delegate = _delegate;
		
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		//选择人数
//		CCLabelTTF *labelTitle = [CCLabelTTF labelWithString:@"选择人数" fontName:@"Arial" fontSize:40];
//		//labelPlayer.color = ccBLUE;
//		labelTitle.position =  ccp( size.width/2 , 450);
//		[self addChild:labelTitle];
		
		//LOGO
		CCSprite *spLogo=[CCSprite spriteWithFile:@"logo2.png"];
		spLogo.position = ccp(SCREEN_WIDTH/2,460);
		[self addChild:spLogo];
		
		CCSprite *spTitle=[CCSprite spriteWithFile:@"游戏人数.png"];
		spTitle.position=ccp(size.width/2 , 425);
		[self addChild:spTitle];
		
		//
		labelPlayer = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d人",app.nPlayerNum] fontName:@"Arial" fontSize:24];
		labelPlayer.color = ccWHITE;
		labelPlayer.position =  ccp( size.width/2 , 400);
		[self addChild:labelPlayer z:0 tag:CARD_GAME_PLAYER];
		
		////////////////////////////////////////////////////////////
		//arrow right
		/*
		rightArrow = [CCMenuItemImage itemFromNormalImage:@"add.png" 
														selectedImage:@"add.png" 
															   target:self 
															 selector:@selector(setPlayerNum:)];
		rightArrow.position = ccp(size.width/2+50, 160);
		rightArrow.tag = CARD_PLAYER_ARROW_RIGHT;
		CCMenu *menu = [CCMenu menuWithItems:rightArrow, nil];
		menu.position = CGPointZero;
		//menu.tag = CARD_PLAYER_ARROW_RIGHT;
		[self addChild:menu];
		
		
		//arrow left
		leftArrow = [CCMenuItemImage itemFromNormalImage:@"remove.png" 
													   selectedImage:@"remove.png" 
															  target:self 
															selector:@selector(setPlayerNum:)];
		leftArrow.position = ccp(size.width/2-50, 160);
		leftArrow.tag = CARD_PLAYER_ARROW_LEFT;
		CCMenu *menuLeft = [CCMenu menuWithItems:leftArrow, nil];
		menuLeft.position = CGPointZero;
		//menuLeft.tag = CARD_PLAYER_ARROW_LEFT;
		[self addChild:menuLeft];
		*/
		//////////////////////////////////////////////////
		
		//底部背景
		CCSprite *bottomBg=[CCSprite spriteWithFile:@"bottom_bg.png"];
		bottomBg.position = ccp(SCREEN_WIDTH/2,BOTTOM_HEIGHT);
		[self addChild:bottomBg];
		
		//开始
		CCMenuItem *startMenu = [CCMenuItemImage itemFromNormalImage:@"btn_start.png" 
													   selectedImage:@"btn_start_down.png" 
															  target:self 
															selector:@selector(OnStartCardGame:)];
		startMenu.position = ccp(size.width/2,BOTTOM_HEIGHT);
		CCMenu *menuCardBack = [CCMenu menuWithItems:startMenu, nil];
		menuCardBack.position = CGPointZero;
		//menuCardBack.tag = E_BASE_LEFT_ARROW_ICON;
		[self addChild:menuCardBack];
		
		//返回
		CCMenuItemImage *vm = [CCMenuItemImage itemFromNormalImage:@"back_icon.png" selectedImage:nil target:self selector:@selector(Card_PlayerSelected_Return:)];	
		CCMenu *vmm = [CCMenu menuWithItems: vm, nil];
		vmm.position = ccp(BOTTOM_X, BOTTOM_HEIGHT);
		[self addChild:vmm z:2];
		
		//help
		
		CCMenuItemImage *help = [CCMenuItemImage itemFromNormalImage:@"help_icon.png" selectedImage:nil target:self selector:@selector(Card_PlayerSelected_Des:)];	
		CCMenu *helpMenu = [CCMenu menuWithItems: help, nil];
		helpMenu.position = ccp(SCREEN_WIDTH - BOTTOM_X , BOTTOM_HEIGHT);
		[self addChild:helpMenu z:2];
		
		
		//////////////////////////////////////////////
		playerInfoArray = [[NSMutableArray alloc] initWithCapacity:0];
		
		//nPlayerNum = 4; //默认为4
		
		[self schedule:@selector(showTextField) interval:0.8f];
		
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changNum:)
		 /*收到消息后的响应函数*/ name:@"ChangNum"
		 /*消息名字，在发消息时　　指定的*/ object:nil];
		
	}
	
	return self;
}

-(void)changNum:(NSNotification*)notification
{
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
	
	if([[notification object] isKindOfClass:[NSString class]])
	{
		NSString * ss = [notification object];
		if ([ss intValue] == CARD_PLAYER_ARROW_LEFT)
		{
			//[self showTextFieldWithIndex:nPlayerNum];
			[labelPlayer setString:[NSString stringWithFormat:@"%d",app.nPlayerNum]];
		}
		else
		{
			//[self showTextFieldWithIndex:nPlayerNum];
			[labelPlayer setString:[NSString stringWithFormat:@"%d",app.nPlayerNum]];
		}
	}
}


-(void)OnStartCardGame:(id)sender
{
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
	//[app.pTextField setHidden:YES];	
	[app setTextFieldArrayVisable:YES];
	
	[self savePlayerInfo];
	
	if(delegate)
	{
		[delegate Card_PlayerSelected:ECard_PlayerSelected_Play PlayerNum:app.nPlayerNum];
	}
	
	/*
	CCScene *scene = [CCScene node];   
	Card_Game *game= [Card_Game node];
	game.nTotalPlayer = nPlayerNum;
	[scene addChild:game];   
	[[CCDirector sharedDirector] replaceScene:[CCTransitionShrinkGrow transitionWithDuration:1.2f scene:scene]];
	*/
}

//-(void)textFieldDown:(id)sender
//{
//	[(UITextField*)sender resignFirstResponder]; //取消键盘
//	
//	[(UITextField*)sender removeFromSuperview];
//}

//设置玩家人数
-(void)setPlayerNum:(id)sender
{
	/*
	int ntag = [sender tag];
	
	if (ntag == CARD_PLAYER_ARROW_LEFT)
	{
		if (nPlayerNum > 2)
		{
			nPlayerNum --;
			[self showTextFieldWithIndex:nPlayerNum];
			[labelPlayer setString:[NSString stringWithFormat:@"%d",nPlayerNum]];
		}
	}
	else
	{
		if (nPlayerNum < 8)
		{
			nPlayerNum ++;
			[self showTextFieldWithIndex:nPlayerNum];
			[labelPlayer setString:[NSString stringWithFormat:@"%d",nPlayerNum]];
		}
	}
	*/
}

-(void)showTextField
{
	[self unschedule:@selector(showTextField)];
	
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
	
	//[app.pTextField setHidden:NO];
	
	[app setTextFieldArrayVisable:NO];
	
	//[self showTextFieldWithIndex:4];
}

//显示供用户输入的UITextField
-(void)showTextFieldWithIndex:(int)index
{
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];

	for(int i = 0;i < [app.textFieldArray count]; i++)
	{
		if (i<index)
		{
			[(UITextField*)[app.textFieldArray objectAtIndex:i] setHidden:NO];
		}
		else
			[(UITextField*)[app.textFieldArray objectAtIndex:i] setHidden:YES];
	}
	
	if (index > 4 && index < 7) 
	{
		//startMenu.position=ccp(SCREEN_WIDTH/2,120-15*(index-4));
		
		//rightArrow.position = ccp(SCREEN_WIDTH/2+50, 160-15*(index-4));
		//leftArrow.position  = ccp(SCREEN_WIDTH/2-50, 160-15*(index-4));
	}
	if (index > 4)
	{
		[app.scrollView setContentOffset:CGPointMake(0, 40*(index-4)) animated:YES]; 
	}
	else 
	{
//		CGRect viewFrame = [app.scrollView frame];  
//		viewFrame.origin.y -= 45*(index-4);//keyboardSize.height;  
//		app.scrollView.frame = viewFrame;  
	}
}

//保存玩家信息
-(void)savePlayerInfo
{
	NSMutableDictionary * diction = [[NSMutableDictionary alloc] initWithCapacity:0];
	
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
	
	for(int i = 0;i < [app.textFieldArray count]; i++)
	{
		//[diction setObject:<#(id)anObject#> forKey:<#(id)aKey#>]
#if 1
		NSDictionary *subD;
		if ([((UITextField*)[app.textFieldArray objectAtIndex:i]).text length] >0)
		{
			subD = [NSDictionary dictionaryWithObjectsAndKeys:
								  ((UITextField*)[app.textFieldArray objectAtIndex:i]).text,@"name",
								  [NSString stringWithFormat:@"%d",0],@"score",
								  nil];
		}
		else
		{
			subD = [NSDictionary dictionaryWithObjectsAndKeys:
								  [NSString stringWithFormat:@"玩家%d",i+1],@"name",
								  [NSString stringWithFormat:@"%d",0],@"score",
								  nil];
		}


			
		[diction setObject:subD forKey:[NSString stringWithFormat:@"Player_%d",i]];
			NSLog(@"Player_%d",i);
#endif
	}
	
	//NSString * filePath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"txt"];
	NSArray  * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString * documentsDirectory = [paths objectAtIndex:0];
	NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@"CardPlayer"];
	
	[diction writeToFile:filePath atomically:YES];
	NSLog(@"saveUserLayer============== end");
}

-(void)Card_PlayerSelected_Des:(id)sender
{
	if(delegate)
	{
		AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
		
		[app setTextFieldArrayVisable:YES];
		
		[delegate Card_PlayerSelected:ECard_PlayerSelected_Des PlayerNum:0];
	}
}

-(void)Card_PlayerSelected_Return:(id)sender
{
	if(delegate)
	{
		[delegate Card_PlayerSelected:ECard_PlayerSelected_Return PlayerNum:0];
	}
}

#pragma mark UITextField
- (BOOL)textFieldShouldReturn:(UITextField*)textField 
{
	//Terminate editing 
	[textField resignFirstResponder];  
	return YES; 
} 

- (void)textFieldDidEndEditing:(UITextField*)textField 
{ 
	NSLog(@"Card_Player textFieldDidEndEditing");
//    if (textField == pTextField) {    
//		NSLog(@"%@", pTextField.text);  
//		
//		[pTextField endEditing:YES]; 
//        //[pTextField removeFromSuperview];     
//		// here is where you should do something with the data they entered   
//		  
//	}
} 

- (void)layerReplaced
{
	
}

@end
